package top.vanchor.myblog2back.controller.admin;


import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import top.vanchor.myblog2back.entity.BackAuthGroup;
import top.vanchor.myblog2back.entity.BackAuthRule;
import top.vanchor.myblog2back.service.IBackAuthGroupService;
import top.vanchor.myblog2back.service.IBackAuthRuleService;
import top.vanchor.myblog2back.service.impl.BackAuthGroupServiceImpl;
import top.vanchor.myblog2back.utils.JWTUtils;
import top.vanchor.myblog2back.utils.JwtInterceptor;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ding
 * @since 2022-01-27
 */
@RestController
@RequestMapping("/admin/adminRule")
public class BackAuthRuleController {

    @Resource
    private IBackAuthGroupService groupService;
    @Resource
    private IBackAuthRuleService service;

    @ApiOperation("添加/编辑菜单")
    @ResponseBody
    @PostMapping("/doEdit")
    public JSONObject doEdit(@RequestBody BackAuthRule bean){
        JSONObject jsonObject = new JSONObject();
        String role = groupService.getById(Integer.parseInt(Objects.requireNonNull(JWTUtils.getUserId(JwtInterceptor.getTokenInfo())))).getRole();
        if (!"SUPER_ADMIN".equals(role)){
            jsonObject.put("code",250);
            jsonObject.put("msg","你无权操作");
            return jsonObject;
        }
        //判断
        int editNum = 0;
        boolean isAdd = bean.getRuleId() == null;
        editNum += service.doEdit(bean);
        if (editNum > 0){
            jsonObject.put("code",200);
            jsonObject.put("msg",isAdd?"添加成功":"编辑成功");
        }else{
            jsonObject.put("code",510);
            jsonObject.put("msg","操作失败");
        }
        return jsonObject;
    }

    @ApiOperation("删除管理员")
    @ResponseBody
    @PostMapping("/doDelete")
    public JSONObject doDelete(@RequestBody JSONObject data){
        JSONObject jsonObject = new JSONObject();
        String role = groupService.getById(Integer.parseInt(Objects.requireNonNull(JWTUtils.getUserId(JwtInterceptor.getTokenInfo())))).getRole();
        if (!"SUPER_ADMIN".equals(role)){
            jsonObject.put("code",250);
            jsonObject.put("msg","你无权操作");
            return jsonObject;
        }
        //处理前端数据
        String[] idsStr = String.valueOf(data.get("ids")).split(",");
        List<Integer> ids = new ArrayList<>();
        for (String s : idsStr) {
            ids.add(Integer.parseInt(s));
        }
        //遍历，确认不含有特殊菜单，否则return
        int beforeIdsSize = ids.size();
        for (int i = 0; i < ids.size() ;i++) {
            System.err.println(ids.get(i));
            if (1 == service.getById(ids.get(i)).getDisabled()){
                ids.remove(i --);
            }
        }
        if (ids.size() < beforeIdsSize){
            jsonObject.put("code",500);
            jsonObject.put("msg","选择中含有"+(beforeIdsSize - ids.size())+"个特殊菜单（不可删除），请取消相应的勾选");
            return jsonObject;
        }
        //初始化
        int delNum = 0;
        //执行
        try {
            delNum += service.doDeleteBy(ids);
            if (delNum >= ids.size()){
                jsonObject.put("code",200);
                jsonObject.put("msg","已删除"+delNum+"个嵌套（含）菜单");
            }else if (delNum > 0){
                jsonObject.put("code",200);
                jsonObject.put("msg","部分删除成功");
            }else{
                jsonObject.put("code",500);
                jsonObject.put("msg","服务器异常，删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code",500);
            jsonObject.put("msg","服务器异常，操作失败");
        }
        return jsonObject;
    }
}
